package com.woniuxy.hrp.service.Serviceimpl;

import com.woniuxy.hrp.entity.Wallet;
import com.woniuxy.hrp.entity.Walletopr;
import com.woniuxy.hrp.mapper.WalletMapper;
import com.woniuxy.hrp.mapper.WalletoprMapper;
import com.woniuxy.hrp.service.WalletService;
import com.woniuxy.hrp.utils.UUIDUtil;
import com.woniuxy.hrp.utils.UtilDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * @Description：
 * @Author：曹杰克
 * @CreateTime：2022/8/29 22:19
 */
@Service("walletService")
public class WalletServiceImpl implements WalletService {
    @Autowired
    WalletMapper walletMapper;
    @Autowired
    WalletoprMapper walletoprMapper;

    @Override
    public Wallet getWallet(Wallet wallet) {
        Wallet wallet1 = walletMapper.getWallet(wallet);
        return wallet1;
    }
/**
 * desc: 对钱包余额的增减
 * @Param: [wallet]
 * @Return: [com.woniuxy.hrp.entity.Wallet]
 * @Author: caojieke
 * @Date: 2022/8/30 9:55
 */
    @Override
    public void updateWallet(Wallet wallet) {
        //查出对应的wallet
        Wallet wallet1 = walletMapper.getWallet(wallet);
        //对余额进行操作
        BigDecimal num=wallet1.getBalance().add(wallet.getBalance());
        wallet1.setBalance(num);
        //更新到数据库中
        walletMapper.update(wallet1);
        //更新钱包操作记录
        Walletopr walletopr=new Walletopr();
        String dateTime = UtilDateTime.getDateTime();
        walletopr.setDatetime(dateTime);
        walletopr.setWay("钱包支付");
        String s = UUIDUtil.generate_UUID();
        walletopr.setOrderId(s);
        walletopr.setProject("预约挂号");
        walletopr.setFee(wallet.getBalance());
        walletopr.setStatus("y");
        walletoprMapper.insert(walletopr);
    }

    @Override
    public Wallet getByPatientId(int id) {
        return walletMapper.getByPatientId(id);
    }

    /**
     * desc: 增加钱包（注册用户时钱包一起生成）
     * @Param: [wallet]
     * @Return: [com.woniuxy.hrp.entity.Wallet]
     * @Author: shanshanfan
     * @Date: 2022/8/31 16:50
     */
    @Override
    public void insert(Wallet wallet) {
        walletMapper.insert(wallet);
    }
}
